"""
created  by  hzwlxy  at 2018/7/4 11:11
__author__: 西瓜哥
__QQ__ : 120235331
__Note__： 管理员相关验证
"""
import re
from wtforms import StringField, RadioField, SelectField
from wtforms.validators import ValidationError, InputRequired, Length, Email

__author__ = '西瓜哥'

from .base import Base


class AdminLoginForm(Base):
    name = StringField(validators=[InputRequired(message="请输入用户名"), Length(4, 20, message="用户名长度4-20位")])
    password = StringField(validators=[InputRequired(message="请输入密码"), Length(4, 20, message="密码长度4-20位")])


class AdminPasswordForm(Base):
    oldpassword = StringField(validators=[InputRequired(message="请输入旧密码"), Length(6, 20, message="旧密码长度6-20位")])
    password = StringField(validators=[InputRequired(message="请输入新密码"), Length(6, 20, message="新密码长度6-20位")])
    repassword = StringField(validators=[InputRequired(message="请确认新密码")])

    def validate_repassword(self, field):
        if self.password.data != field.data:
            raise ValidationError('两次密码不一致')
        return True


class AdminProfileForm(Base):
    nick_name = StringField(validators=[InputRequired(message="请输入昵称"), Length(3, 20, message="昵称长度3-20位")])
    sex = RadioField(choices=[('1', 'man'), ('2', 'woman'), ('0', 'unknow')])
    chinese_name = StringField()
    phone = StringField()
    qq = StringField()
    remarks = StringField(validators=[Length(0, 255, message="备注字符长度最大255字节")])
    email = StringField(validators=[Email(message="邮箱格式不正确")])

    def validate_chinese_name(self, field):
        if field.data:
            regex = re.compile(r'^[\u4e00-\u9fa5]{2,10}$')
            if not regex.match(field.data):
                raise ValidationError('真实姓名为2-12位中文字符')
        return True

    def validate_phone(self, field):
        if field.data:
            regex = re.compile(r'^[1][3,4,5,7,8][0-9]{9}$')
            if not regex.match(field.data):
                raise ValidationError('手机号码格式不正确')
        return True

    def validate_qq(self, field):
        if field.data:
            regex = re.compile(r'^\d{5,12}$')
            if not regex.match(field.data):
                raise ValidationError('QQ号码为5位以上数字')
        return True
